Make WorkspaceClient.dbutils lazy via cached_property#1470
Merged
Conversation
Regenerated ``databricks/sdk/__init__.py`` with the updated template (imports ``functools.cached_property``, drops the eager ``self._dbutils = _make_dbutils(self._config)`` from ``__init__``, emits ``dbutils`` as a ``@cached_property`` that calls ``_make_dbutils`` on first access). Adds four ``tests/test_client.py`` tests that lock in the contract: - ``dbutils`` is a ``functools.cached_property`` descriptor on ``WorkspaceClient``. - ``WorkspaceClient.__init__`` does not invoke ``_make_dbutils``. - The first ``ws.dbutils`` read invokes ``_make_dbutils`` once; subsequent reads return the cached value without re-invoking. - Constructing ``WorkspaceClient`` on a faked Spark Connect runtime (whose ``dbruntime`` raises ``CONTEXT_UNAVAILABLE_FOR_REMOTE_CLIENT`` on any namespace materialization) succeeds without importing ``databricks.sdk.runtime`` at all — the durable sidestep of databricks/dbt-databricks#1252. Complements #1469 (which catches the same failure at runtime-module import time as a defense-in-depth fallback).
96b01a9 to
b7a21f1
Compare
|
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
hectorcast-db
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes
WorkspaceClient.dbutilsafunctools.cached_propertyso consumers that never read it pay no construction cost — and, on Spark Connect runtimes, never touch the legacySparkContextpath thatdatabricks.sdk.runtimematerializes on import. Includes four regression tests that lock in the contract.Why
WorkspaceClient.__init__used to call_make_dbutils(self._config)eagerly, which on a cluster importsdatabricks.sdk.runtime. On a Spark Connect (shared-access-mode) cluster, that import materializes a legacySparkContextand raisesCONTEXT_UNAVAILABLE_FOR_REMOTE_CLIENT, crashing the constructor before any API call. Downstream consumers that never touch.dbutils(notablydbt-databricksPython models) hit this for no reason — see #1463 and databricks/dbt-databricks#1252.#1469 patches the runtime side as a defense-in-depth fallback (catch the materialization failure, fall back to
RemoteDbUtils). This PR is the durable fix: callers that don't read.dbutilsnever trigger the build at all, sidestepping the entire code path. The first read still calls_make_dbutilsonce, lazily; subsequent reads hit the cached attribute in__dict__at plain-attribute speed.What changed
databricks/sdk/__init__.py(generated from updated template):from functools import cached_propertyadded to the imports.self._dbutils = _make_dbutils(self._config)line is removed from__init__.@property def dbutils(which returned the cachedself._dbutils) becomes@cached_property def dbutilsthat calls_make_dbutils(self._config)on first access._dbutilswas a private attribute with no external consumers (verified across the codebase), so removing it does not break any public surface.tests/test_client.py— four new tests:test_dbutils_is_a_cached_property— descriptor type check.test_workspace_client_init_does_not_build_dbutils— spies_make_dbutils, constructs aWorkspaceClient, asserts the spy was never called.test_dbutils_first_access_builds_exactly_once— first read invokes_make_dbutilsonce (returns the spy's sentinel); second read still showscall_count == 1and same identity.test_workspace_client_constructs_on_spark_connect_without_touching_runtime— fakesdbruntimeto raiseCONTEXT_UNAVAILABLE_FOR_REMOTE_CLIENTon any namespace materialization; assertsWorkspaceClient(config=...)succeeds anddatabricks.sdk.runtimeis never imported during construction. This is the strongest evidence that the dbt-databricks failure mode is sidestepped by this change alone.How is this tested?
tests/test_client.pyautospec tests untouched, still pass.databricks.sdk.runtimeis not insys.modulesafterWorkspaceClient(config=...)— i.e., the constructor literally does not reach for the runtime module.NO_CHANGELOG=true